$(function(){
    var count = 10;// 默认一次检索10条记录
    $.get('user/getStaffInfo', function (data) {
        var staff = data.data.staff;
        var department = data.data.department;
        $('#department-name').text(department.deptName+'后台管理');
        console.log('staff-->' + staff.name);
        new Vue({
            el:'#header-avatar',
            data:{
                staff:staff.name
            },
            components: {
                'avatar': Avatar.Avatar
            }
        });
    });
    new Vue({
        el:"#app",
        data:{
            grades: [],
            reports: [],
            currentGrade: {},
            condition: -1,
            currentPage: 1,
            pageNum:0,
            currentStu:{}
        },
        methods:{
            changeGrade(grade) {
                this.currentGrade = grade;
                this.pageNum = (grade.headcount % count) == 0?(grade.headcount / count):((Math.floor(grade.headcount / count) + 1));
                console.log("pageNum-->"+this.pageNum);
                this.pageSelect(grade.gradeId, 1, this.condition);
            },
            changeCondition(condition) {
                if (condition == -1) {
                } else if (condition == 0) {
                    this.pageNum = (this.currentGrade.submittedCount % count) == 0?(this.currentGrade.submittedCount / count):((Math.floor(this.currentGrade.submittedCount / count) + 1));
                } else {
                    let unsubmittedCount = this.currentGrade.headcount - this.currentGrade.submittedCount;
                    this.pageNum = (unsubmittedCount % count) == 0?(unsubmittedCount / count):((Math.floor(unsubmittedCount / count) + 1));
                }
                console.log(this.pageNum);
                this.condition = condition;
                this.pageSelect(this.currentGrade.gradeId, this.currentPage,condition);
            },
            changeCurrentPage(pno) {
                console.log('当前页号-->' + pno);
                this.currentPage = pno;
                this.pageSelect(this.currentGrade.gradeId, pno,this.condition);
            },
            exportExcel() {
                window.location.href = "report/outReportRecords?currentGradeId="+ this.currentGrade.gradeId;
            },
            pageSelect(currentGradeId,currentPage,condition) {
                this.$http.get('report/pageSelect',
                    {params:{count:count,currentGradeId:currentGradeId,currentPage:currentPage,submitted:condition}}).then(function(res){
                    console.log(res.body);
                    let result = res.body.data;
                    this.reports = result.reports;
                    this.currentPage = currentPage;
                },function(){
                    console.log('请求失败！');
                });
            },
            delReport() {
                console.log("删除-->" + this.currentStu);
                this.$http.post('user/clearReportRecord',
                    {stuId:this.currentStu.stuId}, {emulateJSON:true}).then(function(res){
                    console.log(res.body);
                    let result = res.body;
                    if (result.success) {
                        this.pageSelect(this.currentGradeId, this.currentPage, this.condition);
                    }
                },function(){
                    console.log('请求失败！');
                });
            }
        },
        created() {
            this.$http.get('report/pageSelect',
                {params:{currentPage:this.currentPage, submitted: this.condition}}).then(function(res){
                console.log(res.body);
                let result = res.body.data;
                this.reports = result.reports;
                this.grades = result.grades;
                this.pageNum = (this.grades[0].headcount % count) == 0?(this.grades[0].headcount / count):((Math.floor(this.grades[0].headcount / count) + 1));
                this.currentGrade = result.grades[0];
            },function(){
                console.log('请求失败！');
            });
        }
    });

    $('.logout').click(function () {
        $.get('user/logout', function (data) {

        }) ;
        window.location.href = 'backLogin.action';
    });
});